<?php
/**
 * Initialize:
 *  - all checkbox is checked
 *  - $_SESSION['key_id_removed'] = null
 * When checkbox is unchecked then push value into $_SESSION['key_id_removed']
 */
/*--------------------------------------------------------------
 * MODEL
 *-------------------------------------------------------------*/
  /**
   * @return array
   */
class Model
{
  public $stateKeyRemoved = 'key_id_removed'; //fix here
  
  /**
   * @return array
   */
  public function getStateKeyAll()
  {
    $result = array();
    
    //fix here
    
    return $result;
  }
  
  public function addStateKeyRemoved($value)
  {
    $code = $this->getStateKeyRemoved();
    
    if ($value)
    {
      if ($code != '')
        $code .= ';' . $value;
      else
        $code = $value;
      
      $this->setStateKeyRemoved($code);
      return TRUE;
    }
    else
      return FALSE;
  }
  
  public function delStateKeyRemoved($value)
  {
    $stateKeyRemoved = $this->getStateKeyRemoved();
    
    if ($stateKeyRemoved != '')
    {
      $result = array();
      $aStateKeyRemoved = explode(';', $stateKeyRemoved);
      
      foreach ($aStateKeyRemoved as $code)
      {
        if ($code != $value)
          array_push($result, $code);
      }
      
      $this->setStateKeyRemoved(implode(';', $result));
      return TRUE;
    }
    else
      return FALSE;
  }
  
  public function getStateKeyRemoved()
  {
    return Yii::app()->user->getState($this->stateKeyRemoved, '');
  }
  
  public function setStateKeyRemoved($value)
  {
    Yii::app()->user->setState($this->stateKeyRemoved, $value);
    return TRUE;
  }
  
  public function renderCheckBox($data, $row)
  {
    $value = $data["order_id"]; //fix here
    
    $stateKeyRemoved = $this->getStateKeyRemoved();
    $pos             = strpos($stateKeyRemoved, $value);

    if ($stateKeyRemoved!='' && ($pos!==FALSE))
      $checked = false;
    else
      $checked = true;

    return CHtml::checkBox('check_single', $checked, array(
      'id'    => '',
      'value' => $value,
      'class' => 'check_single',
    ));
 }
  
  /**
   * StateKeyFinal = StateKeyAll - StateKeyRemoved
   */
  public function getStateKeyFinal()
  {
    $result           = array();
    $stateKeyAll      = $this->getStateKeyAll();
    $stateKeyRemoved  = $this->getStateKeyRemoved();
    
    if (empty($stateKeyRemoved))
    {
      $result = $stateKeyAll;
    }
    else
    {
      foreach ($stateKeyAll as $code)
      {
        $pos = strpos($stateKeyRemoved, $code);
        if ($pos === FALSE)
          array_push($result, $code);
      }
    }
    
    return $result;
  }
}
/*--------------------------------------------------------------
 * END OF "MODEL"
 *-------------------------------------------------------------*/

/*--------------------------------------------------------------
 * CONTROLLER
 *-------------------------------------------------------------*/
class Controller
{
  public function actionCheckboxProcess($value, $action)
  {
    $model = new Model; //fix here
    
    if ($action == 'add')
      $model->addStateKeyRemoved($value);
    else //action = remove
      $model->delStateKeyRemoved($value);
  }
  
  public function actionCheckboxCheckAll()
  {
    $model = new Model; //fix here
    $model->setStateKeyRemoved(null);
  }
  
  public function actionCheckboxUncheckAll()
  {
    $model = new Model; //fix here
    $value = implode('@', $model->getStateKeyAll());
    $model->setStateKeyRemoved($value);
  }
  
  public function actionCheckboxStatus()
  {
    $model = new Model; //fix here
    echo $model->getStateKeyRemoved();
  }
}
/*--------------------------------------------------------------
 * END OF "CONTROLLER"
 *-------------------------------------------------------------*/

/*--------------------------------------------------------------
 * VIEW
 *-------------------------------------------------------------*/
$columns = array (
  array (
    'type'   => 'raw',
    'value'  => array($model, 'renderCheckBox'),
    'header' => CHtml::checkBox(
      "check_all",
      $model->getStateKeyRemoved() != '' ? false : true
    ),
    'headerHtmlOptions' => array(
      'width' => '25',
      'style' => 'text-align:center;'
    ),
    'htmlOptions' => array(
      'style' => 'text-align:center;'
    ),
  ),
);
?>
<script type="text/javascript">
/*----------------------------------------------------------
 EVENT
 ----------------------------------------------------------*/
$('#check_all').live('click', function(){
  if ($(this).is(':checked'))
  {
    url = "<?php echo Yii::app()->createUrl('controller/checkboxCheckAll') ?>";
    $('#null').load(url, function(){
      $('.check_single').each(function() {
        $(this).attr('checked','checked')
      })
    });
  }
  else
  {
    url = "<?php echo Yii::app()->createUrl('controller/checkboxUncheckAll') ?>";
    $('#null').load(url, function(){
      $('.check_single').each(function() {
        $(this).removeAttr('checked');
      })
    });
  }
});

$('.check_single').live('click', function(){
  var value = $(this).val();
  var action = '';
  
  if ($(this).is(':checked'))
    action = 'remove';
  else
    action = 'add';
  
  url = "<?php echo Yii::app()->createUrl('controller/checkboxProcess') ?>"
    + '/value/' + value
    + '/action/' + action;
  $('#null').load(url, function(){
    setCheckBoxAll();
  });
});

/*----------------------------------------------------------
 FUNCTIONS
 ----------------------------------------------------------*/
function setCheckBoxAll()
{
  var url = "<?php echo Yii::app()->createUrl('controller/checkboxStatus') ?>";
  $('#null').load(url, function (){
    if ($('#null').text() == '')
      $('#check_all').attr('checked','checked');
    else
      $('#check_all').removeAttr('checked');
  });
}
</script>